package com.tim.app.ui.fragment.message;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.application.library.base.BaseFragment;
import com.application.library.runtime.event.EventListener;
import com.application.library.runtime.event.EventManager;
import com.application.library.widget.smarttab.SmartTabLayout;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.constant.EventTag;
import com.tim.app.db.dao.DaoFactory;
import com.tim.app.server.entry.Notice;
import com.tim.app.ui.adapter.TabAdapter;

/**
 * 通知页面
 */
public class NoticeContainerFragment extends BaseFragment implements SmartTabLayout.TabProvider {

    public static final String TAG = "NoticeFragment";
    private View rootView;

    private ViewPager vpMyViewContent;
    private TabAdapter pageAdapter;

    private SmartTabLayout viewPagerTab;

    int currentIndex = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == rootView) {
            rootView = inflater.inflate(R.layout.fragment_notice_container, container, false);

            vpMyViewContent = (ViewPager) rootView.findViewById(R.id.vpMyViewContent);

            pageAdapter = new TabAdapter(getChildFragmentManager());
            pageAdapter.addFragment(NoticeFragment.newInstance(AppKey.NOTICE_TYPE_COMMENT), getString(R.string.notice_comment));
            pageAdapter.addFragment(NoticeFragment.newInstance(AppKey.NOTICE_TYPE_AGREE), getString(R.string.notice_prise));
            pageAdapter.addFragment(NoticeFragment.newInstance(AppKey.NOTICE_TYPE_FANS), getString(R.string.notice_fans));
            pageAdapter.addFragment(NoticeFragment.newInstance(AppKey.NOTICE_TYPE_ANSWER), getString(R.string.notice_answer));
            pageAdapter.addFragment(NoticeFragment.newInstance(AppKey.NOTICE_TYPE_INVITIE), getString(R.string.notice_invite));
            vpMyViewContent.setOffscreenPageLimit(5);
            vpMyViewContent.setAdapter(pageAdapter);
            vpMyViewContent.setCurrentItem(0);

            viewPagerTab = (SmartTabLayout) rootView.findViewById(R.id.tab_layout);
            viewPagerTab.setCustomTabView(this);
            viewPagerTab.setViewPager(vpMyViewContent);
            setNoticeViewState(0, View.INVISIBLE);
            viewPagerTab.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    currentIndex = position;
                    if (position == 0) {
                        DaoFactory.getNoticeDao().updataReadState(AppKey.NOTICE_TYPE_COMMENT);
                    } else if (position == 1) {
                        DaoFactory.getNoticeDao().updataReadState(AppKey.NOTICE_TYPE_AGREE);
                    } else if (position == 2) {
                        DaoFactory.getNoticeDao().updataReadState(AppKey.NOTICE_TYPE_FANS);
                    } else if (position == 3) {
                        DaoFactory.getNoticeDao().updataReadState(AppKey.NOTICE_TYPE_ANSWER);
                    } else if (position == 4) {
                        DaoFactory.getNoticeDao().updataReadState(AppKey.NOTICE_TYPE_INVITIE);
                    }
                    setNoticeViewState(position, View.INVISIBLE);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }

        EventManager.ins().registListener(EventTag.ON_NEW_NOTICE, eventListener);
        showUnReadNoticeStated();
        return rootView;
    }

    EventListener eventListener = new EventListener() {
        @Override
        public void handleMessage(int what, int arg1, int arg2, Object dataobj) {
            switch (what) {
                case EventTag.ON_NEW_NOTICE:
                    Notice notice = (Notice) dataobj;
                    String typeValue = "";
                    if (notice.getOperation().equals("action")) {
                        typeValue = notice.getActionType();
                    } else {
                        typeValue = notice.getOperation();
                    }

                    if (typeValue.equals(AppKey.NOTICE_TYPE_COMMENT)) {
                        if (currentIndex == 0) {
                            DaoFactory.getNoticeDao().updataReadState(AppKey.TYPE_COMMENT);
                        } else {
                            setNoticeViewState(0, View.VISIBLE);
                        }
                    } else if (typeValue.equals(AppKey.NOTICE_TYPE_AGREE)) {
                        if (currentIndex == 1) {
                            DaoFactory.getNoticeDao().updataReadState(AppKey.NOTICE_TYPE_AGREE);
                        } else {
                            setNoticeViewState(1, View.VISIBLE);
                        }
                    } else if (typeValue.equals(AppKey.NOTICE_TYPE_FANS)) {
                        if (currentIndex == 2) {
                            DaoFactory.getNoticeDao().updataReadState(AppKey.NOTICE_TYPE_FANS);
                        } else {
                            setNoticeViewState(2, View.VISIBLE);
                        }
                    } else if (typeValue.equals(AppKey.NOTICE_TYPE_ANSWER)) {
                        if (currentIndex == 3) {
                            DaoFactory.getNoticeDao().updataReadState(AppKey.NOTICE_TYPE_ANSWER);
                        } else {
                            setNoticeViewState(3, View.VISIBLE);
                        }
                    } else if (typeValue.equals(AppKey.NOTICE_TYPE_INVITIE)) {
                        if (currentIndex == 4) {
                            DaoFactory.getNoticeDao().updataReadState(AppKey.NOTICE_TYPE_INVITIE);
                        } else {
                            setNoticeViewState(4, View.VISIBLE);
                        }
                    }
                    break;
            }
        }
    };

    private void showUnReadNoticeStated() {
        int count = DaoFactory.getNoticeDao().getUnReadMessageCountByType(AppKey.NOTICE_TYPE_COMMENT);
        int count1 = DaoFactory.getNoticeDao().getUnReadMessageCountByType(AppKey.NOTICE_TYPE_AGREE);
        int count2 = DaoFactory.getNoticeDao().getUnReadMessageCountByType(AppKey.NOTICE_TYPE_FANS);
        int count3 = DaoFactory.getNoticeDao().getUnReadMessageCountByType(AppKey.NOTICE_TYPE_ANSWER);
        int count4 = DaoFactory.getNoticeDao().getUnReadMessageCountByType(AppKey.NOTICE_TYPE_INVITIE);
        if (count > 0 && currentIndex != 0) {
            setNoticeViewState(0, View.VISIBLE);
        }
        if (count1 > 0 && currentIndex != 1) {
            setNoticeViewState(1, View.VISIBLE);
        }
        if (count2 > 0 && currentIndex != 2) {
            setNoticeViewState(2, View.VISIBLE);
        }
        if (count3 > 0 && currentIndex != 3) {
            setNoticeViewState(3, View.VISIBLE);
        }
        if (count4 > 0 && currentIndex != 4) {
            setNoticeViewState(4, View.VISIBLE);
        }
    }

    /**
     * 通知的未读状态
     *
     * @param visibility
     */
    private void setNoticeViewState(int position, int visibility) {
        View tab = viewPagerTab.getTabAt(position);
        View mark = tab.findViewById(R.id.custom_tab_notification_mark);
        mark.setVisibility(visibility);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != rootView && null != rootView.getParent()) {
            ((ViewGroup) rootView.getParent()).removeView(rootView);
        }
        EventManager.ins().removeListener(EventTag.ON_NEW_NOTICE, eventListener);
    }


    @Override
    public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
        LayoutInflater inflater = LayoutInflater.from(container.getContext());
        Resources res = container.getContext().getResources();
        View tab = inflater.inflate(R.layout.notice_tab_icon, container, false);
        View mark = tab.findViewById(R.id.custom_tab_notification_mark);
        mark.setVisibility(View.INVISIBLE);
        TextView value = (TextView) tab.findViewById(R.id.custom_text);
        switch (position) {
            case 0:
                value.setText(res.getString(R.string.notice_comment));
                break;
            case 1:
                value.setText(res.getString(R.string.notice_prise));
                break;
            case 2:
                value.setText(res.getString(R.string.notice_fans));
                break;
            case 3:
                value.setText(res.getString(R.string.notice_answer));
                break;
            case 4:
                value.setText(res.getString(R.string.notice_invite));
                break;
        }
        return tab;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }


}
